//
//  NSDate+Extension.swift
//  SinaWb
//
//  Created by Jiang on 5/25/16.
//  Copyright © 2016 Jiang. All rights reserved.
//

import Foundation
extension NSDate{
    /**
        将时间字符串转化为N
     - parameter string:          时间字符串
     - parameter formatterString: 格式
     
     - returns: 转换好的NSDate
     */
   class func formatterDate(string :String,formatterString : String) -> NSDate{
        let formatter = NSDateFormatter()
        formatter.dateFormat = formatterString
        formatter.locale = NSLocale(localeIdentifier:"en")
        return formatter.dateFromString(string)!
    }
    
    ///获取与当前时间对比的字符串
    func descriptionString()->String{
        let formatter = NSDateFormatter()
        let Calendar = NSCalendar.currentCalendar()
        
        if Calendar.isDateInToday(self){
            
            let internval = Int( NSDate().timeIntervalSinceDate(self))
            if internval < 60 {
                return "刚刚"
            }else if internval < 60 * 60{
                return "\(internval / 60)分钟前"
            }else if internval < 60 * 60 * 24{
                return "\(internval / (60 * 60))小时前"
            }
            
        }else if Calendar.isDateInYesterday(self) {
            formatter.dateFormat = "昨天" + "HH:mm"
        }else{
            let compare = Calendar.components(NSCalendarUnit.Year, fromDate: self, toDate: NSDate(), options: NSCalendarOptions.init(rawValue: 0))
            if compare.year >= 1 {
                formatter.dateFormat = "yyyy年MM月dd日 HH:mm"
            }else{
                formatter.dateFormat = "MM月dd日 HH:mm"
            }
        }
        return formatter.stringFromDate(self)
    }

    
}